<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt srcset />
        <span>电商管理系统</span>
      </div>
      <el-button type="warning" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px' : '200px'">
        <div class="fold" @click="toggleFold">
          <i :class="isCollapse? iconList[1] : iconList[0]"></i>
        </div>
        <el-menu
          background-color="#3c6382"
          text-color="#fff"
          active-text-color="#7ed6df"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in rightsList" :key="item.id">
            <template slot="title">
              <!-- 菜单图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 菜单名称 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
              @click="saveNaveState('/'+item1.path)"
            >
              <i class="el-icon-menu"></i>
              {{item1.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 获取到的菜单信息
      rightsList: [],
      // 菜单图标
      iconList: {
        125: 'iconfont icontechreport-',
        103: 'iconfont iconyonghuquanxian',
        101: 'iconfont iconshangpinguanli',
        102: 'iconfont icondingdanguanli',
        145: 'iconfont iconkujialeqiyezhan_shujutongji',
        0: 'iconfont iconcaozuo-zhedie',
        1: 'iconfont iconfoldzhedie'
      },
      isCollapse: false,
      // 默认激活的菜单路径
      activePath: ''
    }
  },
  methods: {
  // 点击退出按钮式退出到登录页面
    logout() {
      // 通过编程式导航跳转到登录页面
      this.$router.push('/login')
      // 清除浏览器存储的授权信息
      window.sessionStorage.clear()
    },
    // 侧边栏的折叠切换
    toggleFold() {
      this.isCollapse = !this.isCollapse
    },
    // 点击侧边选项时将路径保存
    saveNaveState(activePath) {
      // 将路径保存到浏览器本地存储中
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 设定菜单路径
    setNavState() {
      const activePath = window.sessionStorage.getItem('activePath')
      this.activePath = activePath
    },
    // 获取菜单信息
    async getRightsList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.rightsList = res.data
    }
  },
  created() {
    this.getRightsList()
    this.setNavState()
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #636e72;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  > div {
    display: flex;
    justify-content: center;
    align-items: center;
    > span {
      padding-left: 10px;
      color: white;
      font-size: 18px;
    }
  }
}
.el-aside {
  background-color: #3c6382;
  .el-menu {
    border-right: none;
  }
  .fold {
    padding-left: 5px;
    background-color: #3c7396;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.el-main {
  background-color: #dfe6e9;
}
.iconfont {
  margin-right: 10px;
}
</style>
